<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    if (!window.WebSocket){
        alert("browser not support webSocket!");
    }else {
        socket = new WebSocket("ws://localhost:8882/hello");

        socket.addEventListener('open',function (event) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了..";
        });

        socket.addEventListener('message',function (event) {
            var rt = document.getElementById("responseText");
            rt.value =  rt.value + "\n" +"收到服务器端返回的信息：" + event.data;
        });

        socket.addEventListener('close',function (event) {
            var rt = document.getElementById("responseText");
            rt.value =  rt.value + "\n" +"连接关闭了";
        });

    }

    function send(value) {
        if (!socket){
            alert("socket 没创建好");
            return ;
        }
        if(socket.readyState === WebSocket.OPEN){
            socket.send(value);
        }else {
            alert("socket连接还没开启！");
        }

    }

</script>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>